package org.gruposp2p.aula.gwt.client.content;

import org.gruposp2p.aula.gwt.client.AulaApplication;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.i18n.client.Constants;
import org.restlet.gwt.data.Status;

public class GlassedDialog extends DialogBox implements ClickHandler {
	
	public static interface CwConstants extends Constants {	
		String GlassedDialogCaption();
		String CancelButtonCaption();
		String AcceptButtonCaption();
		String LoadingCoursesMessage();
		String LoadingCoursegroupMessage();
		String ConnectionErrorMessage();
		String statusCodeMsg();
		String statusDescriptionMsg();
	}	

    private PopupPanel glass;
	private CwConstants constants;
	private AulaApplication aulaApplication;
	private static GlassedDialog instance; 
    
    private GlassedDialog() {    	
        glass = new PopupPanel();
        glass.setStylePrimaryName("rx-glass"); 
    }

    public static GlassedDialog getInstance () {
    	if (instance == null) {
    		instance = new GlassedDialog();
    	}
    	return instance;
    }
    
    public void initData (AulaApplication aulaApplication) {
    	this.aulaApplication = aulaApplication;
        constants = aulaApplication.getConstants();
    }
    
    public void showErrorGettingResource (Status status) {
    	String errorMessage = constants.ConnectionErrorMessage() + "<br/><br/>" + 
    		constants.statusCodeMsg() + " " + status.getCode() + "<br/>" +
    		constants.statusDescriptionMsg() + " " + status.getDescription() + "<br/>";
		setText(constants.GlassedDialogCaption()); 
		FlexTable layout = new FlexTable(); 
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("300px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    HTML html = new HTML(errorMessage);
	    layout.setWidget(0, 0, html);	    
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);	  	    
	    //layout.setWidget(0, 0, aulaApplication.getImages().loading().createImage());
	    Button acceptButton = new Button(constants.AcceptButtonCaption(), this);	 
	    hPanel.add(acceptButton);
	    layout.setWidget(1, 0, hPanel);
	    cellFormatter.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);	   
	    setWidget(layout);
	    //To solve Firefox strange effect
	    //widget.setFocus(true);  
	    glass.show();
	    show();
	    center();
    }
    
    public void showLoadingCoursesDialog () {
    	showLoadingObjectDialog (constants.LoadingCoursesMessage());
    }  

    public void showLoadingCoursegroupDialog () {
    	showLoadingObjectDialog (constants.LoadingCoursegroupMessage());
    }
    
    private void showLoadingObjectDialog (String objectMessage) { 
		setText(constants.GlassedDialogCaption()); 
		FlexTable layout = new FlexTable(); 
		layout.clear();
	    layout.setCellSpacing(6);
	    layout.setWidth("250px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    HTML html = new HTML(objectMessage);
	    html.setStylePrimaryName("loadingMessage");
	    layout.setWidget(0, 0, html);	  
	    //HorizontalPanel hPanel = new HorizontalPanel();
	    //hPanel.setSpacing(5);	  	    
	    //layout.setWidget(1, 0, hPanel);
	    //cellFormatter.setHorizontalAlignment(
	    //		1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
	    //loadingImage = "<img src=\"" + GWT.getModuleBaseURL() + "images/loading.gif\">";
	    setWidget(layout);
	    //To solve Firefox strange effect
	    //widget.setFocus(true);  
	    glass.show();
	    show();
	    center(); 		
    }
    
	public void hideDialog () {
        glass.hide();		
        hide();
	}
    
	@Override
	public void onClick(ClickEvent event) {
        glass.hide();		
        hide();
	}
	
}